package com.yalantis.phoenix.sample;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yalantis.phoenix.PullToRefreshView;
import com.yalantis.phoenix.RecyclerViewLayoutManager;
import com.yalantis.phoenix.recyclerViewAdapter.RecyclerViewAdapter;

import java.util.Map;

/**
 * Created by Oleksii Shliama.
 */
public class RecyclerViewFragment extends BaseRefreshFragment {

    private PullToRefreshView mPullToRefreshView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_recycler_view, container, false);

        RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new RecyclerViewLayoutManager(getActivity()));
        final RefreshAdapter refreshAdapter = new RefreshAdapter();
        refreshAdapter.dataList = mSampleList;
        recyclerView.setAdapter(refreshAdapter);

        mPullToRefreshView = (PullToRefreshView) rootView.findViewById(R.id.pull_to_refresh);
        mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {  // wosao 刷新结束的回调
                        mPullToRefreshView.setRefreshing(false);
                    }
                }, REFRESH_DELAY);
            }

            @Override
            public void onLoadMore() {
                mPullToRefreshView.postDelayed(new Runnable() {
                    @Override
                    public void run() {  // wosao 刷新结束的回调
                        int size = mSampleList.size();
                        for (int i = 0; i < size; i++) {
                            Map<String, Integer> stringIntegerMap = mSampleList.get(i);
                            mSampleList.add(stringIntegerMap);
                        }
                        refreshAdapter.dataList = mSampleList;
                        refreshAdapter.notifyDataSetChanged();
                        mPullToRefreshView.loadMoreFinish(false);
                    }
                }, REFRESH_DELAY);
            }
        }, R.id.text, R.id.pb_load_more);

        return rootView;
    }

    private class RefreshAdapter extends RecyclerViewAdapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view;
            //根据viewType来判断要创建哪个类型的view
            if(viewType == 0){
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_layout,parent,false);
                return new FooterHolder(view);
            }else {
                view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_item, parent, false);
                return new SampleHolder(view);
            }
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int pos) {
            //通过holder来确定显示的是哪个类型的View
            if(holder instanceof FooterHolder){

            }else {
                SampleHolder sampleHolder = (SampleHolder) holder;
                Map<String, Integer> data = mSampleList.get(pos);
                sampleHolder.bindData(data);
            }
        }
    }

    private class SampleHolder extends RecyclerView.ViewHolder {

        private View mRootView;
        private ImageView mImageViewIcon;

        private Map<String, Integer> mData;

        public SampleHolder(View itemView) {
            super(itemView);

            mRootView = itemView;
            mImageViewIcon = (ImageView) itemView.findViewById(R.id.image_view_icon);
        }

        public void bindData(Map<String, Integer> data) {
            mData = data;

            mRootView.setBackgroundResource(mData.get(KEY_COLOR));
            mImageViewIcon.setImageResource(mData.get(KEY_ICON));
        }
    }

    //FooterView布局的ViewHolder
    public class FooterHolder extends RecyclerView.ViewHolder {
        TextView textView;

        public FooterHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.text);
        }
    }

}
